<?php

namespace app\api\controller;

use app\common\model\Ad;
use Endroid\QrCode\QrCode;
use think\Controller;
use think\Request;
use think\Validate;

class User extends Base
{
    //设置支付密码
    public function setPayPassword()
    {
        $params   = $this->request->param();
        $validate = new Validate([
            'pay_password' => 'require|alphaNum|length:8,25',
        ]);
        if (!$validate->check($params)) {
            return $this->fail($validate->getError(), 40012);
        }
        $user = $this->user();
        if ($user->pay_password) {
            return $this->fail('你已经设置过支付密码了', 40010);
        };

        $pay_password       = $params['pay_password'];
        $user->pay_password = password_hash($pay_password, 1);
        if ($user->save()) return $this->renderJson();
        return $this->fail('设置失败', 40011);
    }

    //当前收益
    public function profit()
    {

    }

    //合约兑换
    public function transfer()
    {

    }

    //邀请
    public function invite()
    {
        $user        = $this->user();
        $invite_code = $user->invite_code;
        $qrCode      = new QrCode('https://d.ucoin.pw/');
        $qr_code     = $qrCode->writeDataUri();
        return $this->renderJson([
            'invite_code' => $invite_code,
            'qr_code' => $qr_code
        ]);
    }

    //系统公告
    public function ads()
    {
        $ads = Ad::order('id', 'desc')->paginate(10);
        return $this->renderJson($ads);
    }


}
